'use strict';
const Service = require('egg').Service;

class ManageService extends Service {
    async status() {
        const { app, ctx } = this;
        const Op = app.Sequelize.Op;
        const where = { upt_act: { [Op.ne]: 'D' } };
        return await ctx.model.Manage.findAndCountAll({
          distinct: true, // 不加distinct，count和实际不符
          where,
          attributes:['status', 'mode']
        });
     }
    async page(query) {
        const { app, ctx } = this;
        const { limit, page } = query;
        const Op = app.Sequelize.Op;
        const where = { upt_act: { [Op.ne]: 'D' } };
        // const roleWhere = {};
        if (query.name) {
          where.name = { [Op.like]: `%${query.name}%` };
        }
        if (query.status) {
          where.status = { [Op.like]: `%${query.status}%` };
        }
        if (query.level) {
          where.level = { [Op.like]: `%${query.level}%` };
        }
        if (query.num) {
          where.num = { [Op.like]: `%${query.num}%` };
        }
        
        return await ctx.model.Manage.findAndCountAll({
          distinct: true, // 不加distinct，count和实际不符
          // include: {
          //   model: ctx.model.Sys.Role,
          //   attributes: [ 'id', 'name', 'code' ],
          //   where: roleWhere,
          // },
          where,
          offset: (page - 1) * limit,
          limit: parseInt(limit),
          order: [[ 'created_at', 'desc' ]],
        });
     }
    async create(body) {
       const {app,ctx} = this;
       const { userid } = ctx.state.user;
       body.created_id = userid;
       body.updated_id = userid;
       try {
        return await ctx.model.transaction(async t => {
          // 创建用户信息
          await ctx.model.Manage.create(body, { transaction: t });
  
          return { success: true };
        });
      } catch (error) {
        console.log(error);
        return { success: false };
      }
    }
    async update(body) {
       const { app,ctx } = this;
       const { userid } = ctx.state.user;
       body.updated_at = ctx.helper.formatTime(new Date());
       body.updated_id = userid;
       body.upt_act = 'U';
       try {
        return await ctx.model.transaction(async t => {
  
          // 更新设备信息
          await ctx.model.Manage.update(body, {
            transaction: t,
            where: {
              id: body.id,
            },
          });
          return { success: true };
        });
      } catch (error) {
        console.log(error);
        return { success: false };
      }
    }
    async delete({id}) {
      const { ctx } = this;
      const body = {
        upt_act: 'D',
        updated_at: ctx.helper.formatTime(new Date()),
        updated_id: ctx.state.user.userid,
      };
      try {
        return await ctx.model.Manage.update(body, {
          where: { id },
        });
      } catch (error) {
        console.log(error);
        return null;
      }
    }
    async query() {
      const { app, ctx } = this;
      const Op = app.Sequelize.Op;
      const where = { upt_act: { [Op.ne]: 'D' } };
      return await ctx.model.Manage.findAndCountAll({
        distinct: true, // 不加distinct，count和实际不符
        where,
      });
    }

}
module.exports = ManageService;